/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.market.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.market.entity.EventLotteryDrawHistory;
import com.kaaction.modules.market.service.EventLotteryDrawHistoryService;

/**
 * 抽奖记录Controller
 * @author zengbiao
 * @version 2017-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/market/eventLotteryDrawHistory")
public class EventLotteryDrawHistoryController extends BaseController {

	@Autowired
	private EventLotteryDrawHistoryService eventLotteryDrawHistoryService;
	
	@ModelAttribute
	public EventLotteryDrawHistory get(@RequestParam(required=false) String id) {
		EventLotteryDrawHistory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eventLotteryDrawHistoryService.get(id);
		}
		if (entity == null){
			entity = new EventLotteryDrawHistory();
		}
		return entity;
	}
	
	@RequiresPermissions("market:eventLotteryDrawHistory:view")
	@RequestMapping(value = {"list", ""})
	public String list(EventLotteryDrawHistory eventLotteryDrawHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EventLotteryDrawHistory> page = eventLotteryDrawHistoryService.findPage(new Page<EventLotteryDrawHistory>(request, response), eventLotteryDrawHistory); 
		model.addAttribute("page", page);
		return "modules/market/eventLotteryDrawHistoryList";
	}

	@RequiresPermissions("market:eventLotteryDrawHistory:view")
	@RequestMapping(value = "form")
	public String form(EventLotteryDrawHistory eventLotteryDrawHistory, Model model) {
		model.addAttribute("eventLotteryDrawHistory", eventLotteryDrawHistory);
		return "modules/market/eventLotteryDrawHistoryForm";
	}

	@RequiresPermissions("market:eventLotteryDrawHistory:edit")
	@RequestMapping(value = "save")
	public String save(EventLotteryDrawHistory eventLotteryDrawHistory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eventLotteryDrawHistory)){
			return form(eventLotteryDrawHistory, model);
		}
		eventLotteryDrawHistoryService.save(eventLotteryDrawHistory);
		addMessage(redirectAttributes, "保存抽奖记录成功");
		return "redirect:"+Global.getAdminPath()+"/market/eventLotteryDrawHistory/?repage";
	}
	
	@RequiresPermissions("market:eventLotteryDrawHistory:edit")
	@RequestMapping(value = "delete")
	public String delete(EventLotteryDrawHistory eventLotteryDrawHistory, RedirectAttributes redirectAttributes) {
		eventLotteryDrawHistoryService.delete(eventLotteryDrawHistory);
		addMessage(redirectAttributes, "删除抽奖记录成功");
		return "redirect:"+Global.getAdminPath()+"/market/eventLotteryDrawHistory/?repage";
	}

}